<?php
/**
 * Created by PhpStorm.
 * User: xing
 * Date: 2018/10/1
 * Time: 下午5:59
 */
namespace app\wechat\controller;

use think\Config;
use think\Db;
use app\enumerations\CustomerCurrentLevelEnum;
use app\enumerations\MissionStatusEnum;
use app\enumerations\MissionTypeEnum;

class Scene extends Common {

    /**
     * 场景列表
     * @return \think\response\Json
     */
    public function sceneList() {

        $currentCustomer = $this->request->currentCustomer;

        $sceneList = $this->sceneService->getSceneListForApiTwo();

        $returnData['list'] = $sceneList;

        return successReturn($returnData);

    }

    /**
     * 开店
     * @return \think\response\Json
     */
    public function openShop() {

        $currentCustomer = $this->request->currentCustomer;
        $sceneId = $this->request->post("sceneId");

        if (!isNullOrEmpty($currentCustomer['home_json'])
            && !isNullOrEmpty($currentCustomer['background_image'])) {
            return errorReturn('errorWechat.errorSceneExist');
        }

        $scene = $this->sceneService->findById($sceneId);

        if (isNullOrEmpty($scene)) {
            return errorReturn('errorWechat.errorSceneNotFound');
        }

        // 门店首页json
        $homeJson['bgId'] = (string)$scene['id'];
        $homeJson['imgList'] = [];

        $customerData['background_image'] = $scene['icon_path'];
        $customerData['home_json'] = json_encode($homeJson);
        $customerData['current_level'] = CustomerCurrentLevelEnum::getEnumValue(CustomerCurrentLevelEnum::STORE_CODE);

        Db::startTrans();
        try {

            $this->customerService->updateByIdAndData(
                $currentCustomer['id'], $customerData);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            return errorReturn('errorWechat.errorService');

        }

        // 添加异步延迟任务-临时添加新店长添加一条收益订单
        if($currentCustomer["current_level"]
            == CustomerCurrentLevelEnum::getEnumValue(CustomerCurrentLevelEnum::ORDINARY_CODE)) {

            // 生成当前时间3-5小时候的某一时间
            $beginTime = date("Y-m-d H:i:s", strtotime("+3 hour"));
            $endTime = date("Y-m-d H:i:s", strtotime("+5 hour"));

            $executeTime = randomDate($beginTime, $endTime);

            $missionData = [
                'mission_code' => generateMissionCode(),
                'param' => json_encode([
                    'task_type' => 'giftCommissionFirstStep',
                    'customer_id' => $currentCustomer["id"]
                ]),
                'execute_time' => $executeTime,
                'mission_type' => MissionTypeEnum::getEnumValue(MissionTypeEnum::SETTIMEOUT_CODE),
                'status' => MissionStatusEnum::getEnumValue(MissionStatusEnum::WAITING_CODE)
            ];

            $this->missionService->saveByData($missionData);

            // 设置延迟任务
            setTimeoutTask($executeTime, $missionData["mission_code"]);

        }

        return successReturn();

    }

}